//
//  ST_String+Crypto.swift
//  BTVideo
//
//  Created by DouLei on 2019/7/10.
//  Copyright © 2019 DouLei. All rights reserved.
//

import Foundation

import CommonCrypto



extension String {
    
    var md5_string: String {
        let data = Data(self.utf8)
        let hash = data.withUnsafeBytes { (bytes: UnsafeRawBufferPointer) -> [UInt8] in
            var hash = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))
            CC_MD5(bytes.baseAddress, CC_LONG(data.count), &hash)
            return hash
        }
        return hash.map { String(format: "%02x", $0) }.joined()
    }
    
    var safe_base_64_data:Data?{
        
        var aa = self.replacingOccurrences(of: "-", with: "+")
        aa = aa.replacingOccurrences(of: "_", with: "/")
        
        let remainder = aa.count % 4
        
        var padding = ""
        if remainder > 0 {
            padding = String(repeating: "=", count: 4 - remainder)
        }
        
        return Data(base64Encoded: aa + padding,
                    options: .ignoreUnknownCharacters)
        
    }
    
    func ni_m_url() -> String {
        return self.replacingOccurrences(of: "https://", with: "http://", options: .literal, range: nil)
    }
    
}
